# -*- coding: utf-8 -*-

'''
动态加载类或者模块
'''
import os,imp

def get_class_def_from_single_class_file(absolute_filename):
	'''根据传入的py文件绝对路径(类名和py文件名要一致)得到类定义'''
	file_path=os.path.dirname(absolute_filename)+os.sep
	class_name=absolute_filename.replace(file_path, "").replace(".py", "")
	fn_, path, desc=imp.find_module(class_name, [file_path])
	loaded_model=imp.load_module(class_name, fn_, path, desc)
	class_def=getattr(loaded_model, class_name)
	return class_def

def scan_modules_files_under_path(file_path):
	'''扫描一个路径下的所有py文件，每个文件算一个module，剔除__init__.py'''
	dirpath=list()
	dirnames=[]
	model_filenames=set()

	for each_dirpath, each_dirnames, each_filenames in os.walk(file_path):
		dirpath.append(each_dirpath)
		dirnames.extend(each_dirnames)
		model_filenames.update(each_filenames)
		pass#遍历得到文件夹下的所有模型文件名

	model_filenames.discard('__init__.py')
	return model_filenames